package org.example.config;

import org.example.utils.BusinessException;
import org.example.utils.Code;
import org.example.utils.Result;
import org.example.utils.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {

//统一管理异常

    //@ExceptionHandler用于设置当前处理器类对应的异常类型
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员,ex对象发送给开发人员
        return new Result(null,222,ex.getMessage());
    }

    //自定义异常
    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        return new Result(null,ex.getCode(),ex.getMessage());
    }

//    除了自定义的异常处理器，保留对Exception类型的异常处理，用于处理非预期的异常
    @ExceptionHandler(Exception.class)
    public Result deException(Exception ex){
        return new Result("系统繁忙，请稍后再试！",Code.SYSTEM_UNKNOW_ERR,ex);
    }

}
